package crp.core.security.domain.services.caller;

import com.qwlabs.auditing.AuditorIdProvider;
import com.qwlabs.security.Caller;
import crp.core.security.Callers;
import io.quarkus.arc.Unremovable;
import io.quarkus.security.identity.SecurityIdentity;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import java.util.Optional;

@Unremovable
@RequestScoped
public class CallerAuditorIdProvider implements AuditorIdProvider {
    private final SecurityIdentity securityIdentity;

    @Inject
    public CallerAuditorIdProvider(SecurityIdentity securityIdentity) {
        this.securityIdentity = securityIdentity;
    }

    @Override
    public Optional<String> get() {
        Caller caller = Caller.load(securityIdentity);
        return Optional.ofNullable(caller)
                .map(Callers::userId);
    }
}
